package AsLayers.Control
{
	import AsLayers.BaseType.Pixel;
	import AsLayers.Map;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Button;
	import mx.controls.VSlider;
	import mx.events.SliderEvent;

	public class Navigation implements IControl
	{
		private var display:Canvas=new Canvas()
		private var panTop:Button
		private var panBottom:Button
		private var panLeft:Button
		private var panRight:Button
		private var zoomBar:VSlider
		private var map:Map
		private var canDragMap:Boolean
		private var sPoint:Pixel
		private var ePoint:Pixel
		public function Navigation()
		{
			this.display.x=3
			this.display.y=3
			this.panTop=new Button();
			this.panBottom=new Button();
			this.panLeft=new Button();
			this.panRight=new Button();

			panTop.x=32
			panTop.y=17
			panTop.width=25
			panTop.height=20
			panBottom.x=32
			panBottom.y=62
			panBottom.width=25
			panBottom.height=20
			panLeft.x=16
			panLeft.y=40
			panLeft.width=25
			panLeft.height=20
			panRight.x=46.5
			panRight.y=40
		    panRight.width=25
			panRight.height=20
			this.zoomBar=new VSlider();
			zoomBar.x=25;
			zoomBar.y=82
			zoomBar.width=39
			zoomBar.allowTrackClick=true
			zoomBar.snapInterval=1
            this.display.addChild(panTop)
            this.display.addChild(panBottom)
            this.display.addChild(panLeft)
            this.display.addChild(panRight)
            this.display.addChild(zoomBar)
		}

		public function setMap(map:Map):void{
			map.getControlerContainer().addChild(this.display);
			map.getLayerContainer().addEventListener(MouseEvent.MOUSE_DOWN,mapMouseDown);
			map.getLayerContainer().addEventListener(MouseEvent.MOUSE_MOVE,mapMouseMove);
			map.getLayerContainer().addEventListener(MouseEvent.MOUSE_UP,mapMouseUp);
			map.getLayerContainer().addEventListener(MouseEvent.DOUBLE_CLICK,mapDoubleClick);
			map.getLayerContainer().addEventListener(MouseEvent.MOUSE_WHEEL,mapWheel);
            this.map=map;
            this.zoomBar.minimum=0;
            this.zoomBar.maximum=this.map.getNumZoomLevels()-1;
            this.zoomBar.addEventListener(SliderEvent.CHANGE,zoomBarChanged);
            this.panTop.addEventListener(MouseEvent.CLICK,panTopClicked);
            this.panBottom.addEventListener(MouseEvent.CLICK,panBottomClicked);
            this.panLeft.addEventListener(MouseEvent.CLICK,panLeftClicked);
            this.panRight.addEventListener(MouseEvent.CLICK,panRightClicked);
		}
		private function mapWheel(e:MouseEvent):void{
			if(e.delta>0)
				this.map.zoomIn();
			else
				this.map.zoomOut();
		}
		private function mapDoubleClick(e:MouseEvent):void{
			
		}
		private function mapMouseDown(e:MouseEvent):void{
			this.canDragMap=true;
			sPoint=new Pixel(e.stageX,e.stageY);
		}
		private function mapMouseMove(e:MouseEvent):void{
			if(this.canDragMap){
				ePoint=new Pixel(e.stageX,e.stageY);
				this.map.pan(-(this.ePoint.x-this.sPoint.x),-(this.ePoint.y-sPoint.y));
				sPoint=ePoint;
			}
		}
		private function mapMouseUp(e:MouseEvent):void{
			this.canDragMap=false;
		}
		private function panTopClicked(e:Event):void{
			this.map.pan(0,-50)
		}
		private function panBottomClicked(e:Event):void{
			this.map.pan(0,50)
		}
		private function panLeftClicked(e:Event):void{
			this.map.pan(-50,0)
		}
		private function panRightClicked(e:Event):void{
			this.map.pan(50,0)
		}
		private function zoomBarChanged(e:Event):void{
			this.map.zoomTo(this.zoomBar.value);
		}
		public function show():void
		{

		}
		
		public function hide():void
		{

		}
		
	}
}